package cn.edu.sjtu.se.dslab.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.data.AvionicsEquipment;
import cn.edu.sjtu.se.dslab.component.data.EquipmentCategory;
import cn.edu.sjtu.se.dslab.service.IAvionicsEquipmentService;
import cn.edu.sjtu.se.dslab.service.IEquipmentCategoryService;
import cn.edu.sjtu.se.dslab.util.Const;

@Controller
@RequestMapping(value = "/domain_entity/equipment")
public class AvionicsEquipmentController {
	@Resource(name="equipmentCategoryService")
	IEquipmentCategoryService equipmentCategoryService;
	
	@Resource(name="avionicsEquipmentService")
	IAvionicsEquipmentService avionicsEquipmentService;
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView getAvionicsEquipmentList(@CookieValue(value = "domain_type", defaultValue = "equipment") String domainType,
			@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		model.setViewName("domain_entity");
		try {
			Cookie foo = new Cookie("domain_type", "equipment"); //bake cookie	
			response.addCookie(foo); //put cookie in response 
			PageBean<AvionicsEquipment> bean = avionicsEquipmentService
					.getAvionicsEquipmentByPageNum(pageNum);
			model.addObject("bean", bean);
			return model;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return model;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addAvionicsEquipment() {
		ModelAndView model = new ModelAndView();
		model.setViewName("add_equipment");
		try {
			List<EquipmentCategory> categoryList = equipmentCategoryService.getAllEquipmentCategoryList();
			model.addObject("categoryList", categoryList);
			return model;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return model;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addAvionicsEquipment(AvionicsEquipment equipment,
			@RequestParam(required = false,defaultValue="0") long equipment_type) {
		logger.info("添加 AvionicsEquipment:" + new Date().toString()
				+ "----start");
		equipment.setType(Const.AVIONICS_EQUIPMENT);
		//System.out.print("equipment_type:"+ equipment_type);
		EquipmentCategory category = equipmentCategoryService.getEquipmentCategoryById(equipment_type);
		equipment.setCategory(category);
		avionicsEquipmentService.addAvionicsEquipment(equipment);
		logger.info("添加 AvionicsEquipment:" + new Date().toString()
				+ "----end");
		return "redirect:/domain_entity/equipment";
	}
}
